package com.sskj.mine.ui.fragment;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.OtherRecordBean;
import com.sskj.mine.presenter.OtherRecordFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.MINE_FRAGMENT_OTHER_RECORD)//帐单记录，其他
public class OtherRecordFragment extends BaseFragment<OtherRecordFragmentPresenter> {
    @Autowired
    String code;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    private MVCCoolHelper<List<OtherRecordBean>> mvcCoolHelper;

    @Override
    protected int getLayoutId() {
        return R.layout.mine_fragment_other_record;
    }

    @Override
    public OtherRecordFragmentPresenter getPresenter() {
        return new OtherRecordFragmentPresenter();
    }

    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(getActivity())
                .setFirstDraw(false)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 2))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent)));
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_other_record, new SlimInjector<OtherRecordBean>() {
            @Override
            public void onInject(OtherRecordBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvNum, data.getMoney())
                        .text(R.id.tvTime, data.getCreateTime())
                        .text(R.id.tvRemark, data.getDetail())
                ;

            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<OtherRecordBean> dataSource = new ModelRx2DataSource<>(
                (ModelRx2DataSource.OnLoadSource<OtherRecordBean>)
                        page -> mPresenter.getOtherRecord(page, 10, code), 10);
        mvcCoolHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcCoolHelper.setDataSource(dataSource);
        mvcCoolHelper.setAdapter(slimAdapter);
        mvcCoolHelper.refresh();
    }
}
